﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using laboration2.Models.Abstract;

namespace laboration2.Models.Repositories
{
    public class EFRepository : IRepository
    {
        private AdventureWorksAssignmentEnteties _enteties = new AdventureWorksAssignmentEnteties();



        #region IRepository Members

        public void AddContact(Contact contact)
        {
            _enteties.ContactSet.AddObject(contact);
        }

        public void DeleteContact(Contact contact)
        {
            this._enteties.DeleteObject(contact);
        }

        public void EditContact(Contact contact)
        {
            throw new NotImplementedException();
        }

        /**
         * Funktion som hämtar ett givet antal entiteter, samt från en given plats
         * Skapad för pagingering
         */
        public PagedList<Contact> GetContacts(int skip, int take)
        {
            var enteties = _enteties.ContactSet.OrderByDescending(c => c.ContactID).Skip(skip).Take(take).ToList();

            return new PagedList<Contact>
            {
                Enteties = enteties,
                HasNext = (skip + 10 < 300),
                HasPrevious = (skip > 0)
            };
        }

        public List<Contact> GetContacts()
        {
            return _enteties.ContactSet.Take(20).ToList();
        }

        public Contact GetContactById(int contactId)
        {

            return this._enteties.ContactSet.FirstOrDefault(c => c.ContactID == contactId);
        }

        public void Save()
        {
            this._enteties.SaveChanges();
        }

        #endregion
    }
}